package com.zch.market.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zch.market.service.UserService;

/**
 * 登录控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("LoginController")
public class LoginController{
	//控制器依赖于service对象
	@Autowired
	private UserService us;
	/**
	 * 注销操作
	 * @param request
	 * @return
	 */
	@RequestMapping("doOut")
	public String doOut(HttpServletRequest request){
		//获得session对象
		HttpSession session = request.getSession();
		//失效---session中存的值
		session.invalidate();
		//重定向回登陆页面
		return "login";
	}
	@RequestMapping("doLogin")
	public String doLogin(String userName,String passWord,
			HttpServletRequest request,HttpServletResponse response){
		//获得登录判断的集合对象
		Map<String, Object> map = us.login(userName, passWord);
		System.out.println(map);
		// 判断登录是否成功
		boolean isLogin = (Boolean) map.get("isLogin");
		if (isLogin) {
			HttpSession session = request.getSession();
			// 登录成功,session域中存放用户对象
			session .setAttribute("user1", map.get("user1"));
			return "admin_index";
		} else {
			// 登录失败
			request.setAttribute("errorMsg", map.get("errorMsg"));
			return "login";
		}
	}
}